<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Wb_share extends CI_Controller {
    
    function __construct() {
        parent::__construct();
    }

	public function add_share(){
		$user_id = $this->input->post('user_id');
		if(!$user_id){
			api_failed('empty_user_id');
		}
		$product_id = $this->input->post('product_id');
		if(!$product_id){
			api_failed('empty_product_id');
		}
		$share_content = $this->input->post('share_content');
		if(!$share_content){
			api_failed('empty_share_content');
		}
		$share_post_id = $this->input->post('share_post_id');
		if(!$share_post_id){
			api_failed('empty_share_post_id');
		}

        $this->load->model('wb_share_model');
        if($this->wb_share_model->has_share($user_id, $product_id))api_failed('has_share');

        $data = array('share'=>$this->wb_share_model->add_share($user_id,
			$product_id, $share_content, $share_post_id, null));
        api_success($data);
	}

	public function del_share(){
		api_failed('invalid operate');
		$share_id = $this->input->post('share_id');
		if(!$share_id){
			api_failed('empty_share_id');
		}
        $this->load->model('wb_share_model');
        $data = array('share'=>$this->wb_share_model->del_share($share_id));
        api_success($data);
	}

	public function set_share(){
		$share_id = $this->input->get('share_id');
		if(!$share_id){
			api_failed('empty_share_id');
		}
		$sets = $this->input->post(null, true);
        $this->load->model('wb_share_model');
        $data = array('share'=>$this->wb_share_model->set_share($share_id, $sets));
        api_success($data);
	}

    public function get_product_share_list() {
		$product_id = $this->input->get('product_id');
		if(!$product_id){
			api_failed('empty_product_id');
		}
		$start_index = $this->input->get('start_index');
		if(!$start_index){
			api_failed('empty_start_index');
		}
		$step = $this->input->get('step');
		if(!$step){
			api_failed('empty_step');
		}
        $this->load->model('wb_share_model');
        $data = array('sum'=>$this->wb_share_model->get_sum_production_share($product_id),
			'share_list'=>$this->wb_share_model->get_product_share_list($product_id, $start_index, $step, null));
        api_success($data);
    }

	public function get_display_share_list(){
        $this->load->model('wb_share_model');
        $data = array('share_list'=>$this->wb_share_model->get_display_share_list());
        api_success($data);
	}

	public function get_product_display_share_list(){
		$product_id = $this->input->get('product_id');
		if(!$product_id){
			api_failed('empty_product_id');
		}
        $this->load->model('wb_share_model');
        $data = array('share_list'=>$this->wb_share_model->get_product_display_share_list($product_id));
        api_success($data);
	}
}